var _ = require('lodash')
const path = require('path')
const Ajv = require('ajv')
var ajvCheck = new Ajv({ removeAdditional: true })
const loadData = require('./../libs/loadData')
const datas = loadData(path.join(__dirname, '../data'))
var t1;

module.exports = function (client, serviceId, schema, topics) {
  console.log(typeof(schema))
  if(typeof(schema) === 'boolean') {
    if(schema === false) {
      clearInterval(t1)
    } else {
      console.log('nothing!')
    }
  } else {
    _.forEach(topics, function(topic) {
      const service = _.find(datas, {
        name: topic.name
      })
      if (topic.topic.retain) {
        console.log(topic.name)
        if(service && topic.check(service.payload)) {
          if(service.payload.enable) { 
            const checks = ajvCheck.compile(schema)
            if(checks(service.settings)) {
              t1 = setInterval(function () {
                var topics = _.concat(_.initial(topic.name.split('/')), 'trigger').join('/')
                client.publish(topics, JSON.stringify(service.payload), {qos: 0, retain: true})
              }, (service.settings.interval)*1000)
            } else {
              console.log('invalid settings')
            }
          } else {
            console.log('unenabled')
            clearInterval(t1)
            client.publish(topic.name, JSON.stringify(service.payload), {qos: 0, retain: true})
          }
        } else {
          console.log('invalid payload')
        } 
      }
    })
  }
  
}

function outtime(topicName, check, t1) {
  console.log('outtime')
  const service = _.find(datas, {
    name: topicName
  })
  if(check(service.payload) && service.payload.enable) {
    return
  } else {
    clearInterval(t1)
  }
}

function time1(client, serviceId, schema, topics, datas) {
  const service = _.find(datas, {
    type: serviceId
  })
  const checks = ajvCheck.compile(schema)
  if(service && checks(service.settings)) {
    caoZuo(client, schema, topics, datas)
  } else {
    console.log('invalid settings: ' + service.settings.interval)
    action(client, topics, datas) 
  }
}

function caoZuo1(client, schema, topics, datas) {
  _.forEach(topics, function(topic) {
    const service = _.find(datas, {
      name: topic.name
    })
    if (topic.topic.retain) {
      console.log(topic.name)
      if(service && service.payload.enable) {
        if(topic.check(service.payload)) { 
          setInterval(function() {
            client.publish(topic.name, JSON.stringify(service.payload), {qos: 0, retain: true})
          }, (service.settings.interval)*1000)
        } else {
          setTimeout(function() {
            client.publish(topic.name, JSON.stringify(service.payload), {qos: 0, retain: true})
          }, (schema.properties.interval.default)*1000)
        }
      } else {
        console.log('unenabled')
        if(topic.check(service.payload)) {
          client.publish(topic.name, JSON.stringify(service.payload), {qos: 0, retain: true})
        } else {
          client.publish(topic.name, JSON.stringify(topic.data), {qos: 0, retain: true})
        }
        
      } 
    }
  })
}

function action1(client, topics, datas) {
  _.forEach(topics, function(topic) {
    const service = _.find(datas, {
      name: topic.name
    })
    if (topic.topic.retain) {
      console.log(topic.name)
      if(service && topic.check(service.payload)) {
        client.publish(topic.name, JSON.stringify(service.payload), {qos: 0, retain: true})
      } else {
        client.publish(topic.name, JSON.stringify(topic.data), {qos: 0, retain: true})
      } 
    }
  })
}